﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ExcelLibrary;
using Microsoft.Office.Interop;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Excel = Microsoft.Office.Interop.Excel;
using ExcelLibrary;
using ExcelLibrary.SpreadSheet;
using System.Collections.ObjectModel;
using cecbData;
using cecbClasses;

namespace CECB
{
    /// <summary>
    /// Interaction logic for LeopoldCalculation.xaml
    /// </summary>
    public partial class LeopoldCalculation : Window
    {


        string destFile;
        string modlDestFile;

        Microsoft.Office.Interop.Excel.Application oXL;
        Microsoft.Office.Interop.Excel._Workbook oWB;
        Microsoft.Office.Interop.Excel._Worksheet oSheet;

        public LeopoldCalculation()
        {
            InitializeComponent();
        }

        private void button_VwFinalExcel_Click(object sender, RoutedEventArgs e)
        {

            //Excel.Range oRng;
            //Excel.Borders b = null;
            //Excel.Borders fb = null;
            //Excel.Range range;

            //string startColName, endColName;
            //int startColNo, endColNo;


            //oXL = new Excel.Application();
            //oXL.Visible = true;

            ////Get a new workbook.
            //oWB = (Excel._Workbook)(oXL.Workbooks.Add());
            //oSheet = (Excel._Worksheet)oWB.ActiveSheet;

            //// MessageBox.Show(Convert.ToChar(65).ToString());

            //cecbClasses.Data_Adapters.LeoPoldCalculations da = new cecbClasses.Data_Adapters.LeoPoldCalculations();
            //List<Get_Leopold_Phase_Result> LstResult = da.GetLeopoldPhases("PROJ1");
            //List<Get_Leopold_Activities_Result> lstActivities = da.GetLeopoldActivities("PROJ1");
            //List<Get_Leopold_Impacts_Result> lstImpacts = da.GetLeopolImpacts("PROJ1");
            //List<Get_Leopold_Matric_Result> lstMatrice = da.GetLeopolMatrices("PROJ1");


            //// ---- Header Row ---
            //oSheet.Cells[3, 1] = "Category";
            //oSheet.get_Range("A3").ColumnWidth = 15;

            //oSheet.Cells[3, 2] = "Component";
            //oSheet.get_Range("B3").ColumnWidth = 15;

            //oSheet.Cells[3, 4] = "Project Impact";
            //oSheet.get_Range("D3").ColumnWidth = 40;
            //oSheet.get_Range("C3", "D3").Merge();

            //oSheet.get_Range("A3", "D3").Font.Bold = true;
            //oSheet.get_Range("A3", "D3").VerticalAlignment = VerticalAlignment.Center;

            //int z = 5;
            //startColNo = 69;
            //startColName = Convert.ToChar(startColNo).ToString() + "2";

            //// --- Phases Headings -----
            //foreach (Get_Leopold_Phase_Result oRsts in LstResult)
            //{
            //    endColNo = startColNo + (((int)oRsts.NoOfActivities) * 2) - 1;
            //    endColName = Convert.ToChar(endColNo).ToString() + "2";

            //    oSheet.Cells[2, z] = oRsts.actv_phase;

            //    oSheet.get_Range(startColName, endColName).Font.Bold = true;

            //    oSheet.get_Range(startColName, endColName).Merge();
            //    oSheet.get_Range(startColName, endColName).HorizontalAlignment = HorizontalAlignment.Center;

            //    startColNo = endColNo + 1;
            //    startColName = Convert.ToChar(startColNo).ToString() + "2";

            //    z = z + (int)oRsts.NoOfActivities * 2;
            //}

            ////// --- Get Activities ---
            //startColNo = 69;
            //startColName = Convert.ToChar(startColNo).ToString() + "4";

            //int startColNo2 = 69;
            //string startColName2 = Convert.ToChar(startColNo2).ToString() + "3";
            //int endColNo2;
            //string endColName2;

            //int ActNo = 1;
            //z = 5;
            //foreach (Get_Leopold_Activities_Result oRstAct in lstActivities)
            //{
            //    endColNo2 = startColNo2 + 1;
            //    endColName2 = Convert.ToChar(endColNo2).ToString() + "3";

            //    endColNo = startColNo + 1;
            //    endColName = Convert.ToChar(endColNo).ToString() + "4";

            //    oSheet.Cells[3, z] = ActNo;
            //    oSheet.Cells[4, z] = oRstAct.actv_name;

            //    oSheet.get_Range(startColName, endColName).Font.Bold = true;
            //    oSheet.get_Range(startColName, endColName).Orientation = 90;
            //    oSheet.get_Range(startColName, endColName).RowHeight = 200;
            //    oSheet.get_Range(startColName, endColName).WrapText = true;
            //    oSheet.get_Range(startColName, endColName).Merge();

            //    oSheet.get_Range(startColName2, endColName2).Merge();
            //    oSheet.get_Range(startColName2, endColName2).Font.Bold = true;
            //    oSheet.get_Range(startColName2, endColName2).HorizontalAlignment = HorizontalAlignment.Center;


            //    startColNo = startColNo + 2;
            //    startColName = Convert.ToChar(startColNo).ToString() + "4";

            //    startColNo2 = startColNo2 + 2;
            //    startColName2 = Convert.ToChar(startColNo2).ToString() + "3";
            //    z = z + 2;

            //    ActNo = ActNo + 1;
            //}

            //// --- Print Impacts , Magnitude, Imports
            //// --- IMPACTS ---
            //startColNo = 69;
            //startColName = Convert.ToChar(startColNo).ToString() + "5";

            //string srtCatName, endCatName, srtSubName, endSubName, srtImpColNoName, endImpColNoName, srtImpColName, endImpColName;

            //int srtCatNo, endCatNo, srtSubNo;
            //int LineNO = 1;
            //int rowNo = 5;

            //int GrandTotal = 0;

            //string CatName = lstImpacts[0].cmpt_mainCat.ToString();
            //oSheet.Cells[5, 1] = CatName;

            //srtCatNo = 5;
            //srtSubNo = 5;
            //string SubName = lstImpacts[0].cmpt_name.ToString();
            //oSheet.Cells[5, 2] = SubName;

            //foreach (Get_Leopold_Impacts_Result oRstImpacts in lstImpacts)
            //{
            //    z = 5;

            //    if (CatName != oRstImpacts.cmpt_mainCat)
            //    {
            //        srtCatNo = rowNo;
            //        CatName = oRstImpacts.cmpt_mainCat;
            //        oSheet.Cells[rowNo, 1] = CatName;
            //    }

            //    if (SubName != oRstImpacts.cmpt_name)
            //    {
            //        srtSubNo = rowNo;
            //        SubName = oRstImpacts.cmpt_name;
            //        oSheet.Cells[rowNo, 2] = SubName;
            //    }

            //    startColNo = 69;
            //    startColName = Convert.ToChar(startColNo).ToString() + rowNo;

            //    endColNo = startColNo + 1;
            //    endColName = Convert.ToChar(endColNo).ToString() + rowNo;

            //    oSheet.Cells[rowNo, 4] = oRstImpacts.impt_name;

            //    oSheet.Cells[rowNo, 3] = LineNO;
            //    srtImpColNoName = "C" + rowNo;
            //    endImpColNoName = "C" + (rowNo + 1);

            //    srtImpColName = "D" + rowNo;
            //    endImpColName = "D" + (rowNo + 1);

            //    oSheet.get_Range(srtImpColNoName, endImpColNoName).Merge();
            //    oSheet.get_Range(srtImpColName, endImpColName).Merge();


            //    oSheet.get_Range("A" + srtCatNo, "A" + (rowNo + 1)).Merge();

            //    oSheet.get_Range("B" + srtSubNo, "B" + (rowNo + 1)).Merge();


            //    int Total = 0;

            //    // --- MATRIX DATA ----
            //    foreach (Get_Leopold_Activities_Result oRstAct in lstActivities)
            //    {
            //        endColNo = startColNo + 1;
            //        endColName = Convert.ToChar(endColNo).ToString() + rowNo;

            //        var filter = (from i in lstMatrice
            //                      where i.actv_reference == oRstAct.actv_reference && i.impt_reference == oRstImpacts.impt_reference
            //                      select i).ToList();

            //        if (filter.Count() > 0)
            //        {
            //            oSheet.Cells[rowNo, z] = filter[0].mtrx_magnitude;
            //            oSheet.Cells[rowNo + 1, z + 1] = filter[0].mtrx_importance;

            //            Total = Total + (int)(filter[0].mtrx_magnitude * filter[0].mtrx_importance);
            //        }

            //        oSheet.get_Range(startColName, endColName).ColumnWidth = 2.5;

            //        DrawCellBorders(rowNo, z, rowNo, z, true, false, true, false, false);
            //        DrawCellBorders(rowNo, z + 1, rowNo, z + 1, true, false, false, true, true);
            //        DrawCellBorders(rowNo + 1, z, rowNo + 1, z, false, true, true, false, true);
            //        DrawCellBorders(rowNo + 1, z + 1, rowNo + 1, z + 1, false, true, false, true, false);

            //        startColNo = endColNo + 1;
            //        startColName = Convert.ToChar(startColNo).ToString() + rowNo;

            //        z = z + 2;
            //    }

            //    oSheet.Cells[rowNo, z] = Total;
            //    endColName = Convert.ToChar(startColNo) + (rowNo + 1).ToString();
            //    oSheet.get_Range(startColName, endColName).Merge();


            //    GrandTotal = GrandTotal + Total;
            //    rowNo = rowNo + 2;
            //    LineNO = LineNO + 1;
            //}
            //oSheet.Cells[4, z] = "Total";
            //oSheet.Cells[rowNo, z] = GrandTotal;

            //// Calculate Weighted Avg
            //decimal WAGrandTotal = 0;
            //decimal CatWiseWATotal = 0;
            //int catStrLNo = 5;

            //oSheet.Cells[4, z + 1] = "Weighted Average";

            //for (int Lno = 5; Lno <= rowNo - 1; Lno++)
            //{
            //    var cell = (Excel.Range)oSheet.Cells[Lno, z];

            //    if (Convert.ToString(cell.Value2) != null)
            //    {
            //        oSheet.Cells[Lno, z + 1] = (decimal)GrandTotal / (int)cell.Value2;
            //        WAGrandTotal = WAGrandTotal + (decimal)GrandTotal / (int)cell.Value2;

            //        // -- Calculate Total Weighted Avg --
            //        CatWiseWATotal = CatWiseWATotal + ((decimal)GrandTotal / (int)cell.Value2);

            //        if (((Convert.ToString(((Excel.Range)oSheet.Cells[Lno, 1]).Value2)) != null) && (Lno != 5))
            //        {
            //            oSheet.Cells[catStrLNo, z + 2] = CatWiseWATotal;
            //            CatWiseWATotal = 0;
            //        }
            //    }
            //}
            //oSheet.Cells[catStrLNo, z + 2] = CatWiseWATotal;
            //oSheet.Cells[rowNo, z + 1] = WAGrandTotal;

            //oXL.Visible = true;
            //oXL.UserControl = true;

        }

        private void DrawCellBorders(int frmrowNo, int frmColNo, int TorowNo, int ToColNo, bool top, bool bottom, bool left, bool right, bool diagup)
        {
            Microsoft.Office.Interop.Excel.Borders b = null;
            Microsoft.Office.Interop.Excel.Borders fb = null;

            b = (Microsoft.Office.Interop.Excel.Borders)(oSheet.Cells[frmrowNo, frmColNo] as Microsoft.Office.Interop.Excel.Range).Borders;
            fb = (oSheet.Cells[frmrowNo, frmColNo] as Microsoft.Office.Interop.Excel.Range).Borders;


            if (diagup == true)
            {
                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalUp].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalUp].Weight;
            }
            if (top == true)
            {
                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight;
            }
            if (bottom == true)
            {
                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight;
            }
            if (left == true)
            {
                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight;
            }

            if (right == true)
            {
                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight;
            }

        }

        private void Image_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.Close();
        }


        private void button_Closefrm_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void comboBox_ProjRefrnce_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            cecbClasses.Data_Projects ObjProj = new cecbClasses.Data_Projects();
            DataAdapter clsDataAdapter = new DataAdapter();
            ObjProj.Proj_reference = comboBox_ProjRefrnce.SelectedValue.ToString();
            Projects selectedPrj = clsDataAdapter.GetPendingProjectDetailByCode(ObjProj);
            try
            {

                if (ObjProj.Proj_reference != "-1")
                {
                    textBox_projname.Text = selectedPrj.proj_name;
                    textBox_ProjType.Text = selectedPrj.proj_type;
                    textBox_Location.Text = selectedPrj.proj_location;
                    datePicker_FromDate.Text = selectedPrj.proj_fromDate.ToString();
                    datePicker_Date.Text = selectedPrj.proj_toDate.ToString();


                    destFile = selectedPrj.proj_planImage;
                    image_Plan.Source = new BitmapImage(new Uri(destFile));

                    destFile = selectedPrj.proj_planImage;
                    image_model.Source = new BitmapImage(new Uri(modlDestFile));


                    Data_ProjectCriterias ObjProjCri = new Data_ProjectCriterias();
                    ObjProjCri.Proj_reference = comboBox_ProjRefrnce.SelectedValue.ToString();
                    List<ProjectCriteria> selctedMetixCritriaDetails = clsDataAdapter.GetPendingProjMetrixDetailByCode(ObjProjCri);


                    foreach (ProjectCriteria oPrjCrytDetails in selctedMetixCritriaDetails)
                    {

                        if (oPrjCrytDetails.proj_matrix.ToString() == "Leopold")
                        {
                            Labe_Metrix.Content = "Leopold";

                            wrapPanal_Magnitude.Visibility = System.Windows.Visibility.Visible;
                            WrapPanel_Importnce.Visibility = System.Windows.Visibility.Visible;
                            WrapPanel_Simple.Visibility = System.Windows.Visibility.Hidden;

                            if (oPrjCrytDetails.crt_type.ToString() == "Magnitude")
                            {

                                Label_MagnitudeCriteria.Content = oPrjCrytDetails.crt_range.ToString();
                            }



                            else
                            {
                                Label_ImportenceCriteria.Content = oPrjCrytDetails.crt_range.ToString();
                            }
                        }
                        else
                        {

                            Labe_Metrix.Content = "Simple";

                            Label_SimpleCriteria.Content = oPrjCrytDetails.crt_range.ToString();

                            wrapPanal_Magnitude.Visibility = System.Windows.Visibility.Hidden;
                            WrapPanel_Importnce.Visibility = System.Windows.Visibility.Hidden;
                            WrapPanel_Simple.Visibility = System.Windows.Visibility.Visible;
                        }
                    }


                }
            }

            catch
            {

                label_ImageInfo.Content = "No Mathing Image Found";



            }
        }

        public void FillEIAProjectReferences()
        {

            DataAdapter clsDataAdapter = new DataAdapter();
            List<Projects> ProjRefrncesList = clsDataAdapter.GetAllPendingProjRefernces();
            comboBox_ProjRefrnce.ItemsSource = ProjRefrncesList;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FillEIAProjectReferences();

        }

        private void comboBox_ProjTye_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        
    }
}
